import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreDemo {
    public static void main(String[] args) {
        Semaphore semaphoreDemo = new Semaphore(3);

        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                try {
                    semaphoreDemo.acquire();
                    System.out.println(Thread.currentThread().getName() + "\t抢到了车位");
                    TimeUnit.SECONDS.sleep(3);
                    semaphoreDemo.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphoreDemo.release();
                }

            }).start();
        }

    }
}
